#####################################################################
# Cardinality Matching Representative 
#####################################################################

rm(list=ls())

sink("~/Dropbox/Geographic Natural Experiments/02_analyses/02_match1000m.txt")

library(designmatch)
library(stargazer)
library(foreign)
library(gurobi)
library(Hmisc)

##############################################
# Read data and prepare basic output
##############################################

d = read.dta("~/Dropbox/Geographic Natural Experiments/01_data/raw_data/Final-Data.dta")
#d_original = d
names(d)

##############################################
# Prepare data
##############################################

# treatment
table(d$treat)
d$t_ind = 1 - d$treat
table(d$t_ind)

# sort by treatment 
d = d[order(d$t_ind, decreasing=TRUE), ]

# check
table(d$buffer_band)
describe(d$hprice_mean)
describe(d$turnout04)
describe(d$turnout06)
describe(d$male)
d$age = 100-d$birth_year
describe(d$age)

# subset buffer
#table(d$buffer_band)
#d = d[d$buffer_band <= 500,]

##############################################
# Match
##############################################

# Cardinality matching finds the largest matched sample of pairs that meets balance
# requirements. Here the balance requirements are mean balance, fine balance and
# ks balance for different covariates. The solver used is gurobi 6.5.0.

# Treatment indicator
t_ind = d$t_ind

# Moment balance: constrain differences in means to be at most 0.1 standard deviations apart
mom_covs = cbind(d$hprice_mean,d$turnout04,d$turnout06,d$male,d$age)
mom_targets = c(152996,0.6998,0.569,0.478,30)
mom_tols = absstddif(mom_covs, t_ind, .05)
mom = list(covs = mom_covs, tols = mom_tols, targets = mom_targets)

# Solver options
t_max = 60*30
solver = "gurobi"
approximate = 0
solver = list(name = solver, t_max = t_max, approximate = approximate,
              round_cplex = 0, trace = 1)

# Match
out_1 = cardmatch(t_ind, mom = mom, solver = solver)

# Indices of the treated units and matched controls
t_id = out_1$t_id
c_id = out_1$c_id

table(table(t_id))
table(table(c_id))

# Mean balance
cbind(meantab(mom_covs, t_ind, t_id, c_id, digits=3)[, 4:5], round(mom_targets,3))

# Save matched sample 
d_match_city = d[c(t_id, c_id), ]

# Invert treatment
d_match_city$t_ind = 1 - d_match_city$t_ind

# Save samples 
save(d_match_city, file="~/Dropbox/Geographic Natural Experiments/02_analyses/02_match_city_1000m.RData")

sink()
